﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{

    public class AB45GAAUFData : BaseFunction
    {
        public bool flagRegenerateLastMonthData;
        ArrayList ListTris = new ArrayList();

        List<WGSummary> ListWGSummary;
        string TrisInclude = ""; 
        public int ThreadCount; 
        public AB45GAAUFData()
            : base()
        {
            BuildTrisList();  
        }
        public AB45GAAUFData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }
        private void BuildTrisList()
        {
            ListTris = new ArrayList();
            TrisType tristype = null;
            TrisInclude = "01;10;11;13;";
            tristype = new TrisType("01", "+", "净长"); ListTris.Add(tristype);
            tristype = new TrisType("10", "+", "货物收入"); ListTris.Add(tristype);
            tristype = new TrisType("11", "+", "货物收入"); ListTris.Add(tristype);
            tristype = new TrisType("12", "-", "销售返回"); ListTris.Add(tristype);
            tristype = new TrisType("13", "+", "产品录入"); ListTris.Add(tristype);
            tristype = new TrisType("20", "-", "货物发出"); ListTris.Add(tristype);
            tristype = new TrisType("21", "+", "货物进入"); ListTris.Add(tristype);
            tristype = new TrisType("26", "-", "旧布卷减少"); ListTris.Add(tristype);
            tristype = new TrisType("27", "+", "新布卷增加"); ListTris.Add(tristype);
            tristype = new TrisType("31", "-", "发货"); ListTris.Add(tristype);
            tristype = new TrisType("32", "+", "退货"); ListTris.Add(tristype);
            tristype = new TrisType("36", "-", "生产消耗"); ListTris.Add(tristype);
            tristype = new TrisType("40", "-", "等级修改减少"); ListTris.Add(tristype);
            tristype = new TrisType("41", "-", "等级修改增加"); ListTris.Add(tristype);
            tristype = new TrisType("42", "+", "库存修正减少"); ListTris.Add(tristype);
            tristype = new TrisType("43", "+", "库存修正增加"); ListTris.Add(tristype);
            tristype = new TrisType("48", "-", "生产差异-"); ListTris.Add(tristype);
            tristype = new TrisType("49", "+", "生产差异+"); ListTris.Add(tristype);
            tristype = new TrisType("51", "+", "消耗差异"); ListTris.Add(tristype);
            tristype = new TrisType("61", "-", "留位"); ListTris.Add(tristype);
            tristype = new TrisType("62", "+", "取消留位"); ListTris.Add(tristype);
            tristype = new TrisType("64", "+", "留位剪布"); ListTris.Add(tristype);
            tristype = new TrisType("65", "+", "锁定"); ListTris.Add(tristype);
            tristype = new TrisType("66", "+", "解锁"); ListTris.Add(tristype);
            tristype = new TrisType("69", "+", "其他修正"); ListTris.Add(tristype);
            tristype = new TrisType("80", "+", "发票数量"); ListTris.Add(tristype);
            tristype = new TrisType("88", "-", "价值更改-"); ListTris.Add(tristype);
            tristype = new TrisType("89", "+", "价值更改"); ListTris.Add(tristype);
            tristype = new TrisType("IN", "+", "系统自动调节"); ListTris.Add(tristype);

        }
        public DataSet GetRawData(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            DataTable tableLocal = null;
            string KRS,LFD,POS;
            KRS = dwABPOS["KRS"].ToString().Trim();
            LFD = dwABPOS["LFD"].ToString().Trim();
            POS = dwABPOS["POS"].ToString().Trim();
            try
            {
                OracleCommand or1 = OracleDB.CreateCommand();
                #region
                
                or1.CommandText = "SELECT  A.GAAUFKRS||'-'||TO_NUMBER(A.GAAUFLFD)||'-'||TO_NUMBER(A.GAAUFPOS) AS ORDERNO, "
                    + " TRIM(C.LSNAME1) AS FACTORY,TO_CHAR(SYSDATE,'YYYYMMDD') AS ORDERDAT,'' AS ARTICLE, "
                    + " D.ABPSOLLMENGE1*0.01 AS ORDERQTY, "
                    + " TRIM(A.GAART)||'-'||TRIM(A.GAVAR)||'-'||TRIM(A.GAFARBE) AS YARN, "
                    + " TRIM(E.GSAGARNNR)||TRIM(E.GSGARNBEZ)||TRIM(E.GSHANDLNR) AS YARNDES, "
                    + " TRIM(F.GSCBEZ) AS COLORDES,A.GAKG1*0.01 AS REQWEI,A.GAKG3*0.01 AS ALLWEI, "
                    + " (A.GAKG5-A.GAKG6)*0.01 AS REMAINWEI,'' AS SPPART "
                    + " FROM GAAUF A,(SELECT DISTINCT ABFMANDANT,ABFAUFKRS,ABFAUFLFD,ABFAUFPOS,ABFLIEFNR "
                    + " FROM ABFOLGE WHERE TRIM(ABFLIEFNR) IS NOT NULL) B,LIEFDAT C,ABPOS D, "
                    + " GSDAT E,GSCOLOR F "
                    + " WHERE A.GAMANDANT = B.ABFMANDANT AND A.GAAUFKRS = B.ABFAUFKRS "
                    + " AND A.GAAUFLFD = B.ABFAUFLFD AND A.GAAUFPOS = B.ABFAUFPOS "
                    + " AND B.ABFMANDANT = C.LSMANDANT AND B.ABFLIEFNR = C.LSLIEFNR "
                    + " AND A.GAMANDANT = D.ABPMANDANT AND A.GAAUFKRS = D.ABPAUFKRS "
                    + " AND A.GAAUFLFD = D.ABPAUFLFD AND A.GAAUFPOS = D.ABPAUFPOS "
                    + " AND A.GAMANDANT = E.GSMANDANT AND A.GAART = E.GSART "
                    + " AND A.GAVAR = E.GSVAR AND A.GAMANDANT = F.GSCMANDANT(+) "
                    + " AND A.GAART = F.GSCART(+) AND A.GAVAR = F.GSCVAR(+) "
                    + " AND A.GAFARBE = F.GSCFARBE(+) AND A.GASA='723' "
                    + " AND A.GAAUFKRS = '" + KRS + "'"
                    + " AND A.GAAUFLFD = '" + LFD + "'"
                    + " AND A.GAAUFPOS = '" + POS + "'"
                    ;      
                #endregion
                setLocal = Connect2Proxy(or1);
                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "AB45GAAUF";
                    SetDefault(tableLocal);
                   byte[] b;
                    string value; 
                    string sTemp = ""; 
                    string Column = ""; 
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                        Column = "FACTORY";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        #endregion
                        Column = "YARNDES";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        #endregion
                        Column = "COLORDES";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        #endregion
                    }
                    foreach (DataTable dtTemp in setLocal.Tables)
                    {
                        DataSet setUI = new DataSet();
                        setUI = ImportDataTable(setUI, dtTemp);
                        DataTable tableABPOS = setUI.Tables["AB45GAAUF"];
                        foreach (DataRow dwTmp in tableABPOS.Rows)
                        {
                            //setUI = GetABAUFM(setUI, dwTmp);
                            setUI = GetABAUFM(setUI, dwTmp);
                            setUI = GetZUTEI(setUI, dwTmp);
                        }
                        if (setLocal.Tables.Contains("AB45GAAUF"))
                        {
                            setLocal.Tables["AB45GAAUF"].Clear();
                        }
                        setLocal = ImportDataTable(setLocal, setUI.Tables["AB45GAAUF"]);
                    }
                    setLocal.AcceptChanges(); 
                    set4Return.Merge(setLocal);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return set4Return;
        }
        public DataSet GetABAUFM(DataSet dsTarget, DataRow dwABPOS)
        {

            string TableName = "ABAUFM";
            try
            {
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT DISTINCT TRIM(B.ABPART)||'-'||TRIM(B.ABPFARBE)||'-'||B.ABPVAR AS ARTICLE "
                    + " FROM ABAUFM A,ABPOS B "
                    + " WHERE A.ABAMANDANT = B.ABPMANDANT AND A.ABAVKAUFKRS = B.ABPAUFKRS "
                    + " AND A.ABAVKAUFLFD = B.ABPAUFLFD AND A.ABAVKAUFPOS = B.ABPAUFPOS "
                    + " AND A.ABAAUFKRS||'-'||TO_NUMBER(A.ABAAUFLFD)||'-'||TO_NUMBER(A.ABAAUFPOS) = :ORDERNO ";
                orlReadCommand.Parameters.Add("ORDERNO", OracleType.NVarChar).Value = dwABPOS["ORDERNO"].ToString();
                dsResult = Connect2Proxy(orlReadCommand);
                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = "ABAUFM";
                    string ARTICLE = "";
                    foreach (DataRow dwTemp in dsResult.Tables["ABAUFM"].Rows)
                    {
                        ARTICLE += dwTemp["ARTICLE"].ToString() + ";";
                    }
                    dwABPOS["ARTICLE"] = ARTICLE;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return dsTarget;

        }
        public DataSet GetZUTEI(DataSet dsTarget, DataRow dwABPOS)
        {

            try
            {
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT DISTINCT TRIM(ZUSPPART) AS ZUSPPART "
                    + " FROM ZUTEI  WHERE ZUAUFKRS||'-'||TO_NUMBER(ZUAUFLFD)||'-'||TO_NUMBER(ZUAUFPOS) = :ORDERNO "
                    + " AND TRIM(ZUART)||'-'||TRIM(ZUVAR)||'-'||TRIM(ZUFARBE) = :YARN ";
                orlReadCommand.Parameters.Add("ORDERNO", OracleType.NVarChar).Value = dwABPOS["ORDERNO"].ToString();
                orlReadCommand.Parameters.Add("YARN", OracleType.NVarChar).Value = dwABPOS["YARN"].ToString();
                dsResult = Connect2Proxy(orlReadCommand);
                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = "ZUTEI";
                    string SPPART = "";
                    foreach (DataRow dwTemp in dsResult.Tables["ZUTEI"].Rows)
                    {
                        SPPART += dwTemp["ZUSPPART"].ToString() + ";";
                    }
                    dwABPOS["SPPART"] = SPPART;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return dsTarget;

        }
        public DataSet BuildWBBST(DataRow dwUI)
        {
            string progress = "查询完成";
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                EventDispatcher.Instance.RaiseStatus("查询开始", StatusEventType.StartProgress);
                setResult = GetRawData(dwUI);
                setReturn.Merge(setResult);
                EventDispatcher.Instance.RaiseStatus("查询完成", StatusEventType.StopProgress);
            }
            catch (Exception ex)
            {
                Tracer.Error(ABPOS + ex.StackTrace, ex);
            }
            return setReturn;
        }
    }
}